<?php

// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 文件控制器
 * 主要用于下载模型的文件上传和下载
 */
class FileController extends BaseController {

    public function _initialize() {
        
    }

    /* 文件上传 */

    public function upload() {
        $return = array('status' => 1, 'info' => '上传成功', 'data' => '');
        /* 调用文件上传组件上传文件 */
        $File = D('File');
        $file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
        $info = $File->upload(
                $_FILES, C('DOWNLOAD_UPLOAD'), C('DOWNLOAD_UPLOAD_DRIVER'), C("UPLOAD_{$file_driver}_CONFIG")
        );

        /* 记录附件信息 */
        if ($info) {
            $return['data'] = think_encrypt(json_encode($info['download']));
            $return['info'] = $info['download']['name'];
        } else {
            $return['status'] = 0;
            $return['info'] = $File->getError();
        }

        /* 返回JSON数据 */
        $this->success('上传成功', $return);
    }

    /* 下载文件 */

    public function download($id = null) {
        if (empty($id) || !is_numeric($id)) {
            $this->error('参数错误！');
        }

        $logic = D('Download', 'Logic');
        if (!$logic->download($id)) {
            $this->error($logic->getError());
        }
    }

    /**
     * 上传图片
     * @author huajie <banhuajie@163.com>
     */
    public function uploadPicture() {
        //TODO: 用户登录检测

        /* 返回标准数据 */
        $return = array('status' => 1, 'info' => '上传成功', 'data' => '');

        /* 调用文件上传组件上传文件 */
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Picture->upload(
                $_FILES, C('PICTURE_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器

        /* 记录图片信息 */
        if ($info) {
            $return['status'] = 1;
            $return = array_merge($info['download'], $return);
        } else {
            $return['status'] = 0;
            $return['info'] = $Picture->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }

    /**
     * 上传图片
     * @author huajie <banhuajie@163.com>
     */
    public function uploadImage() {
        //TODO: 用户登录检测

        /* 返回标准数据 */
        $return = array('status' => 1, 'info' => '上传成功', 'data' => '');

//        var_dump($_FILES);die;
        /* 调用文件上传组件上传文件 */
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
//        echo $pic_driver;die;
        $info = $Picture->upload(
                $_FILES, C('PICTURE_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器

        /* 记录图片信息 */
        if ($info) {
            $return['success'] = 1;
            $return['msg'] = '上传成功';
            $returndata['path'] = $info['uploads']['path'];
        } else {
            $return['success'] = 0;
            $return['msg'] = $Picture->getError();
        }

        $this->ajaxReturn($return['success'],$return['msg'],$returndata);

        /* 返回JSON数据 */
//        $this->success('上传成功',$return);
    }

    /**
     * 上传视频
     * @author huajie <banhuajie@163.com>
     */
    public function uploadVideo() {
        //TODO: 用户登录检测

        /* 返回标准数据 */
        $return = array('status' => 1, 'info' => '上传成功', 'data' => '');

        /* 调用文件上传组件上传文件 */
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Picture->upload(
                $_FILES, C('VIDEO_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器

       /* 记录图片信息 */
        if ($info) {
            $return['success'] = 1;
            $return['msg'] = '上传成功';
            $returndata['path'] = $info['uploads']['path'];
            
//            $data['video_url'] = $info['uploads']['path'];

            $code = md5(time() + mt_rand(1, 9999));
            $img = '/Uploads/Picture/' . $code . '.jpg';
            $input = dirname(dirname(dirname(dirname(__FILE__)))) . $info['uploads']['path'];
            $output = dirname(dirname(dirname(dirname(__FILE__)))) . $img;

            $returndata['video_img'] = 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $img;
//
            $command = "ffmpeg -i $input -y -f image2 -ss 1 -vframes 1 $output";
            \Think\Log::record("执行命令:" . $command);
            $result = exec($command, $output1);
        } else {
            $return['success'] = 0;
            $return['msg'] = $Picture->getError();
        }

        $this->ajaxReturn($return['success'],$return['msg'],$returndata);
    }

}
